package com.fwh.gmall.dao.impl;

import com.fwh.gmall.dao.UserDao;
import com.fwh.gmall.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public User getUserByUserId(Integer id) {
        User user = jdbcTemplate.queryForObject("select * from t_user where id = ?"
                , new BeanPropertyRowMapper<User>(User.class), id);
        return user;
    }

    @Override
    public List<User> getAllUsers() {
        List<User> users = jdbcTemplate.query("select * from t_user", BeanPropertyRowMapper.newInstance(User.class));
        return users;
    }

    @Override
    public int Save(User user) {
        return jdbcTemplate.update("insert into t_user(name, age, address, create_time, update_time) values(?, ?, ?,?,?)",
                user.getName(),user.getAge(), user.getAddress(),new Date(),new Date());
    }



    @Override
    public int delete(Integer id) {
        return jdbcTemplate.update("DELETE from tb_user where id = ? ",id);
    }
}
